<?php

namespace app\service;

use app\index\model\User;

class Score
{
    public static function add($user_id, $num)
    {
        $res = User::find($user_id)->inc('score', $num)->update();
        if ($res) return 1;
        return 0;
    }
    public static function dec($user_id, $num)
    {
        $res = User::find($user_id)->dec('score', $num)->update();
        if ($res) return 1;
        return 0;
    }
    /**
     * 查询账号的积分是否足够
     * @param int user_id 用户id
     * @param int need 需要的积分
     * @param int isDec(true , false) 当个人积分足够，是否扣除，默认扣除积分
     * 返回结果为还需的积分
     *  */
    public static function compare($user_id, $need, $isDec = true)
    {
        $score = User::find($user_id)->score;
        $payScore = $score - $need;
        if ($payScore < 0) return $payScore;
        if ($isDec) {
            $res = self::dec($user_id, $need);
            if ($res) return 1;
            return 0;
        }
        return 1;
    }
}
